<?php

Loader::using("Models::Entities::Usuarios");

/**
 * Description of Cuenta
 *
 * @author JeyDotC
 * 
 * @export Modules
 * @export-metadata RequiredRoles:Agencia:Linea
 */
class Cuenta extends Module {

    public function index() {
        $this->assign("user", $this->currentUser);
        $this->assign("logout", $this->route->moduleAction("Autenticarse", "logout"));
        $this->assign("action", $this->route->action("passwordChange"));

        $this->display("index");
    }

    public function passwordChange() {
        $originalPass = $this->request->post("originalPasss");
        $newPass = $this->request->post("newPass");
        $newPassConfirm = $this->request->post("newPassConfirm");

        if ($this->currentUser->getCredential() === $originalPass && $newPass === $newPassConfirm) {
            $pass = md5($originalPass);
            $newPass = md5($newPass);

            $this->components->Usuarios->actualizarContrasena($pass, $this->currentUser->getName(), $newPass);

            $this->restartSession($this->currentUser->getName(), $newPassConfirm);
        }

        $this->index();
    }

    private function restartSession($user, $pass) {
        $this->request->getSession()->close();
        if($this->components->Authentication->login($user, $pass)){
            $this->currentUser->setCredential($pass);
            $this->route->redirect($this->route->action("index"));
        }
    }

}

?>
